﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Obra.Sync
{
    public class SyncItemTransferLazyStream
        : ISyncItemTransfer
    {
        private readonly MediaItemMetadata mediaItemMetadata;

        private readonly Func<Stream> lazyStreamFunction;

        private Stream stream;

        public SyncItemTransferLazyStream(
            MediaItemMetadata mediaItemMetadata,
            Func<Stream> lazyStreamFunction)
        {
            this.mediaItemMetadata = mediaItemMetadata;
            this.lazyStreamFunction = lazyStreamFunction;
        }

        #region ISyncItemTransfer Members

        /// <summary>
        /// Data for this media file.
        /// </summary>
        public Stream Stream
        {
            get
            {
                if (this.stream == null)
                {
                    this.stream = this.lazyStreamFunction.Invoke();
                }

                return this.stream;
            }
        }

        /// <summary>
        /// Metadata for this media file.
        /// </summary>
        public MediaItemMetadata MediaItemMetadata
        {
            get { return this.mediaItemMetadata; }
        }

        #endregion
    }
}
